/*************************************************************************
> File Name: greed.cpp
> Author: icebear
> Mail: 3322366195@qq.com
> Created Time: 2023-08-10 22:07:41
> Description: 给定一个罗马数字，将其转换成整数。
************************************************************************/
#include <iostream>
#include <unordered_map>

using namespace std;
class Solution
{
private:
    unordered_map<char, int> symbolValues = {
        {'I', 1},
        {'V', 5},
        {'X', 10},
        {'L', 50},
        {'C', 100},
        {'D', 500},
        {'M', 1000},
    };

public:
    int romanToInt(string s)
    {
        int ans = 0;
        int n = s.length();
        for (int i = 0; i < n; ++i)
        {
            int value = symbolValues[s[i]];
            if (i < n - 1 && value < symbolValues[s[i + 1]]) // 小值在前 减去小值
            {
                ans -= value;
            }
            else
            {
                ans += value;
            }
        }
        return ans;
    }
};
int main(int argc, char const *argv[])
{
    Solution s;
    int value = s.romanToInt("LVIII");
    cout << value << endl;
    return 0;
}
